﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Diagnostics;


/// <summary>
/// AdminService 的摘要说明
/// </summary>
public class AdminService
{

    public AdminService()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 创建一个停止和删除指定IMS服务的xml文件
    /// </summary>
    /// <param name="serviceName">要停止和删除的服务名</param>
    /// <param name="adminXmlFileName">文件保存的完整路径和名称</param>
    /// <returns></returns>
    public bool createRemoveServiceXml(string serviceName, string adminXmlFileName)
    {
        string adminServiceXml = "";
        adminServiceXml += "<?xml version=\"1.0\"?> " + "\n";
        adminServiceXml += "<ADMINCMD version=\"1.0\">  " + "\n";
        adminServiceXml += "<SERVICES> " + "\n";
        adminServiceXml += "<SERVICE type=\"stop\" name=\"" + serviceName + "\" /> " + "\n";
        adminServiceXml += "<SERVICE type=\"remove\" name=\"" + serviceName + "\" /> " + "\n";
        adminServiceXml += "</SERVICES> " + "\n";
        adminServiceXml += "</ADMINCMD> " + "\n";

        try
        {
            StreamWriter rw = File.CreateText(adminXmlFileName);

            rw.Write(adminServiceXml);
            rw.Flush();
            rw.Close();
            return true;
        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message);
            return false;
        }
    }


    /// <summary>
    /// 创建一个添加指定IMS服务的xml文件
    /// </summary>
    /// <param name="serviceName">要添加的服务名</param>
    /// <param name="serviceAxlFileName">要添加的服务对应的AXL文件</param>
    /// <param name="adminXmlFileName">文件保存的完整路径和名称</param>
    /// <returns></returns>
    public bool createAddServiceXml(string imageUrl, string serviceName, string serviceAxlFileName, string adminXmlFileName)
    {
        string adminServiceXml = "";
        adminServiceXml += "<?xml version=\"1.0\"?> " + "\n";
        adminServiceXml += "<ADMINCMD version=\"1.0\">  " + "\n";
        adminServiceXml += "<SERVICES> " + "\n";
        adminServiceXml += "<SERVICE type=\"add\" name=\"" + serviceName + "\" " + "\n";
        adminServiceXml += "axl=\"" + serviceAxlFileName + "\"  " + "\n";
        adminServiceXml += "vsname=\"ImageServer1\" " + "\n";
        adminServiceXml += "imgloc=\"c:\\arcims\\output\" " + "\n";
        //adminServiceXml += "imgurl=\"http://192.168.2.51/output\" " + "\n";//???
        adminServiceXml += "imgurl=\"" + imageUrl + "/\" " + "\n";//???
        adminServiceXml += "imagememory=\"20\" " + "\n";
        adminServiceXml += "cleanup=\"20\" " + "\n";
        adminServiceXml += "imgtype=\"GIF\" /> " + "\n";
        adminServiceXml += "<SERVICE type=\"start\" name=\"" + serviceName + "\" /> " + "\n";
        adminServiceXml += "</SERVICES> " + "\n";
        adminServiceXml += "</ADMINCMD> " + "\n";

        try
        {
            StreamWriter rw = File.CreateText(adminXmlFileName);
            rw.Write(adminServiceXml);
            rw.Flush();
            rw.Close();
            return true;
        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message);
            return false;
        }
    }

    /// <summary>
    /// 创建一个用于重建服务的xml文件
    /// </summary>
    /// <param name="serviceName">要重新加载的服务名</param>
    /// <param name="serviceAxlFileName">要重新加载的服务名所对应的axl文件</param>
    /// <param name="adminXmlFileName">文件保存的完整路径和名称</param>
    /// <returns></returns>
    public bool createResetXml(string imageUrl, string serviceName, string serviceAxlFileName, string adminXmlFileName)
    {
        string adminServiceXml = "";
        adminServiceXml += "<?xml version=\"1.0\"?> " + "\n";
        adminServiceXml += "<ADMINCMD version=\"1.0\">  " + "\n";
        adminServiceXml += "<SERVICES> " + "\n";
        adminServiceXml += "<SERVICE type=\"stop\" name=\"" + serviceName + "\" /> " + "\n";
        adminServiceXml += "<SERVICE type=\"remove\" name=\"" + serviceName + "\" /> " + "\n";
        adminServiceXml += "<SERVICE type=\"add\" name=\"" + serviceName + "\" " + "\n";
        adminServiceXml += "axl=\"" + serviceAxlFileName + "\"  " + "\n";
        adminServiceXml += "vsname=\"ImageServer1\" " + "\n";
        adminServiceXml += "imgloc=\"c:\\arcims\\output\" " + "\n";
        //adminServiceXml += "imgurl=\"http://RSERVER/output\" " + "\n";
        adminServiceXml += "imgurl=" + imageUrl + " " + "\n";
        adminServiceXml += "imagememory=\"4\" " + "\n";
        adminServiceXml += "cleanup=\"20\" " + "\n";
        adminServiceXml += "imgtype=\"PNG\" /> " + "\n";
        adminServiceXml += "<SERVICE type=\"start\" name=\"" + serviceName + "\" /> " + "\n";
        adminServiceXml += "</SERVICES> " + "\n";
        adminServiceXml += "</ADMINCMD> " + "\n";

        try
        {
            StreamWriter rw = File.CreateText(adminXmlFileName);
            rw.Write(adminServiceXml);
            rw.Flush();
            rw.Close();
            return true;
        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message);
            return false;
        }
    }


    /// <summary>
    /// 执行重建地图服务操作，需要确保xmlFileName文件已经事先创建
    /// </summary>
    /// <param name="batFileName">创建的批处理文件路径和名称</param>
    /// <param name="xmlFileName">要执行的服务管理xml文件</param>
    /// <param name="serverUrl" >ArcIMS服务器的url路径</param>
    /// <param name="javaHome">JRE的安装路径</param>
    /// <param name="userName">ArcIMS的管理员用户名</param>
    /// <param name="userPwd">ArcIMS的管理员用户密码</param>
    /// <returns></returns>
    [STAThread]
    public bool Excute(string batFileName,string xmlFileName, string serverUrl, string javaHome, string userName, string userPwd)
    {
        int strBatExcuteStatusCode;
        string batFile = "";
        batFile += "@echo starting to restart Service" + "\n";
        batFile += "@cd " + javaHome + "\n";
        batFile += "@java.exe com.esri.aims.admincore.cmd.Exec \"" + serverUrl + "\" \"" + userName + "\" \"" + userPwd + "\" file \"" + xmlFileName + "\"\n";
        //batFile += "@pause";

        try
        {
            StreamWriter rw = File.CreateText(batFileName);
            rw.Write(batFile);
            rw.Flush();
            rw.Close();

            Process myProcess = new Process();
            myProcess.StartInfo.FileName = batFileName;
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcess.Start();
            myProcess.WaitForExit();
            if (myProcess.HasExited)
            {
                strBatExcuteStatusCode = myProcess.ExitCode;
            }
            else
            {
                myProcess.Kill();
                strBatExcuteStatusCode = myProcess.ExitCode;
            }
            myProcess.Close();

            if (strBatExcuteStatusCode != 0 && strBatExcuteStatusCode != 1)
            {
                return false;
            }
            Log.WriteInfo(xmlFileName + " is been excuted successful!");
            return true;
        }
        catch (FieldAccessException ex)
        {
            Log.WriteException(ex.Message);
            return false;
        }

    }


}
